﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class Delivery : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        private string DInterID;
        private string Did;
        private string Sku;
        private string ID;

        public Delivery()
        {
            InitializeComponent();
        }

        public Delivery(string fno)
        {           
            InitializeComponent();
            DInterID = fno;
        }

        public Delivery(string fno,string _ID)
        {
            InitializeComponent();
            DInterID = fno;
            this.ID = _ID;
        }

        private void Delivery_Load(object sender, EventArgs e)
        {
            if (DInterID == null || DInterID == "")
            {
            }
            else
            {
                DataGridStyle();
                BinData();
            }
        }

        //获取数据
        private void BinData()
        {
            DataSet ds = bjs.GetDelivery(DInterID);
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            if (dt.Rows.Count <= 0)
            {
                return;
            }
            else
            {
                dataGrid1.DataSource = dt;
            }
        }
        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "Delivery";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "序号";
            cl1.MappingName = "Num";
            cl1.Width = 40;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "单号";
            cl2.MappingName = "Did";
            cl2.Width = 50;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "SKU编码";
            cl3.MappingName = "DSKU";
            cl3.Width = 100;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "总数量";
            cl4.MappingName = "Dnumber";
            cl4.Width = 50;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
            cl5.HeaderText = "已扫数量";
            cl5.MappingName = "NDnumber";
            cl5.Width = 50;
            dts.GridColumnStyles.Add(cl5);

            dataGrid1.TableStyles.Add(dts);

        }

        //刷新
        private void button1_Click(object sender, EventArgs e)
        {
            if (DInterID == null || DInterID == "")
            {
            }
            else
            {
                DataGridStyle();
                BinData();
            }
        }
        //返回
        private void button2_Click(object sender, EventArgs e)
        {
            DeliveryMain deM = new DeliveryMain(this.ID);
            deM.Show();
            this.Hide();
        }
        //已扫库存
        private void button4_Click(object sender, EventArgs e)
        {
            if (Did == null || Did == "")
            {
                MessageBox.Show("请选择订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            else 
            {
                DeliveryStk del = new DeliveryStk(DInterID, Did, Sku);
                del.Show();
                this.Hide();
            }
            
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            Did = ((DataTable)(dataGrid1.DataSource)).Rows[index][1].ToString();
            Sku = ((DataTable)(dataGrid1.DataSource)).Rows[index][2].ToString();
        }
        //完成明细
        private void button3_Click(object sender, EventArgs e)
        {
            bool lb = false;

            if (Did == null || Did == "")
            {
                MessageBox.Show("请选择订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            else 
            {
                lb = bjs.FinishDelivery(Did);
                if (lb) 
                {
                    DataGridStyle();
                    BinData();
                } 
                else 
                {
                    MessageBox.Show("操作失败！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
            }

        }


        
    }
}